package org.techmore.hakka.map.location;

import java.util.ArrayList;
import java.util.List;

import org.techmore.hakka.R;

import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.Window;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;

public class MapLocationActitvity extends MapActivity {

	private MapView mapView = null;
	private MapController mapController = null;
	private List<Overlay> overlays = null;
	private MapOverlayView mapOverlayView = null;
	
	private OverlayItem overlayitem  = null;
	
	private String geoPointString = null;
	
	@Override
	protected void onCreate(Bundle icicle) {
		// TODO Auto-generated method stub
		super.onCreate(icicle);
		
		setScreenOrientation();
		
		setContentView(R.layout.map_location);
		
		mapView = (MapView) findViewById(R.id.mapLocationView);
		
		//设置为交通模式
		//mapView.setTraffic(true);
		//设置为卫星模式
		//mMapView.setSatellite(true); 
		//设置为街景模式
		mapView.setStreetView(false);
    	mapView.setEnabled(true);
    	mapView.setClickable(true);
    	mapView.setBuiltInZoomControls(true);
    	
    	mapController = mapView.getController();
    	mapController.setZoom(12); 
    	
    	overlays = mapView.getOverlays();
    	
    	mapOverlayView = new MapOverlayView(this.getResources().getDrawable(R.drawable.location_sign));
    	
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		if(KeyEvent.KEYCODE_BACK == keyCode){
			Intent data = new Intent();
			data.putExtra("geoPointString", geoPointString);
			MapLocationActitvity.this.setResult(RESULT_OK, data);
			MapLocationActitvity.this.finish();
		}
		return super.onKeyDown(keyCode, event);
	}
	
	
	@Override
	public boolean dispatchTouchEvent(MotionEvent event) {
		// TODO Auto-generated method stub
		
		GeoPoint geoPoint = mapView.getProjection().fromPixels((int)event.getX(), (int)event.getY());
		geoPointString = geoPoint.getLatitudeE6()+"-"+geoPoint.getLongitudeE6();
		if(MotionEvent.ACTION_DOWN == event.getAction()){
			mapView.removeAllViews();
			overlays.clear();
			mapOverlayView.clearOverlay();
			overlayitem = new OverlayItem(geoPoint,null,null);
			mapOverlayView.addOverlay(overlayitem);
			overlays.add(mapOverlayView);
		}
		
		return super.dispatchTouchEvent(event);
	}
	
	
	@Override
	protected boolean isRouteDisplayed() {
		// TODO Auto-generated method stub
		return false;
	}
	
	 private void setScreenOrientation(){
	    	Window window = getWindow();
			window.requestFeature(Window.FEATURE_NO_TITLE);
//			window.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
			setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);	//豎屏
//	    	setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);		//橫屏
	    }

}


class MapOverlayView extends ItemizedOverlay<OverlayItem> {
	
	private ArrayList<OverlayItem> overlays = new ArrayList<OverlayItem>();

	public MapOverlayView(Drawable defaultMarker) {
		super(boundCenterBottom(defaultMarker));
	}

	@Override
	protected OverlayItem createItem(int i) {
		// TODO Auto-generated method stub
		return overlays.get(i);
	}

	@Override
	public int size() {
		// TODO Auto-generated method stub
		return overlays.size();
	}
	
	public void addOverlay(OverlayItem overlay) {
		overlays.add(overlay);
		//填充本次
		populate();
	}
	
	public void clearOverlay() {
		overlays.clear();
	}
	
	
	@Override
	protected boolean onTap(int index) {
		
		return true;
	}

}
